package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class VideoController {

    @Autowired
    VideoManager videoManager;

    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){

        //1.先获取用户的id
        Long userId = UserHolder.get().getId();

        //2.调用manager返回pageBeanVo
        PageBeanVo pageBeanVo = videoManager.findRecommendVideoByPage(userId,pageNum,pageSize);

        //3.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }

    @PostMapping("/smallVideos")
    public void save(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
        //2.获取当前登陆者id
        Long userId = UserHolder.get().getId();
        //3.调用manager完成保存
        videoManager.save(userId,videoThumbnail,videoFile);
    }

    //关注
    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public void focusUser(@PathVariable Long focusUserId){
        videoManager.focusUser(focusUserId);
    }

    //取关
    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public void unFocusUser(@PathVariable Long focusUserId){
        videoManager.unFocusUser(focusUserId);
    }

}
